package com.yuexun.dhdoctor.module.meeting.ui;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.format.DateFormat;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;


import com.yuexun.dhdoctor.R;
import com.yuexun.dhdoctor.base.BaseKJActivity;
import com.yuexun.dhdoctor.config.AppConfig;
import com.yuexun.dhdoctor.config.ScreenUtil;
import com.yuexun.dhdoctor.module.meeting.adapter.MeetingAdapter;
import com.yuexun.dhdoctor.module.meeting.bean.MeetingBean;
import com.yuexun.dhdoctor.module.meeting.bean.MeetingBeanDate;
import com.yuexun.dhdoctor.module.meeting.model.impl.MeetingModel;
import com.yuexun.dhdoctor.module.meeting.presenter.IMeetingPresenter;
import com.yuexun.dhdoctor.module.meeting.presenter.impl.MeetingPresenter;
import com.yuexun.dhdoctor.module.meeting.ui.view.IMeetingView;
import com.yuexun.dhdoctor.util.DateUtil;
import com.yuexun.dhdoctor.widget.SearchDialog;

import org.kymjs.kjframe.utils.PreferenceHelper;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;


/**
 * Created by work-jx on 2016/11/15.
 */
public class Act_MeetingInfo extends BaseKJActivity implements IMeetingView {
    @Bind(R.id.tv_week_title)
    TextView tvWeekTitle;
    @Bind(R.id.tv_week_date)
    TextView tvWeekDate;
    @Bind(R.id.lv_meeting)
    ListView lvMeeting;
    @Bind(R.id.tv_empty_tip)
    TextView tvEmptyTip;
    @Bind(R.id.title)
    TextView tvTitle;
    //    @Bind(R.id.ib_right)
//    Button ibRight;
    @Bind(R.id.right)
    TextView meeting_right_tv;

    private Dialog navDialog;
    private boolean interrelated = false;
    private SearchDialog searchDialog;

    private TextView relevanceT;

    private Date nowMeetingWeek;
    private String searchTheme = "";
    IMeetingPresenter presenter;
    BaseAdapter adapter;

    ArrayList<MeetingBeanDate> meetingBeanDates = new ArrayList<MeetingBeanDate>();

    @Override
    public void setRootView() {
        setContentView(R.layout.act_fragment_container);
        ButterKnife.bind(this);
    }

    @Override
    public void initData() {
        super.initData();
        presenter = new MeetingPresenter(new MeetingModel(), this);
//        ibRight.setBackgroundResource(R.drawable.ic_nav_more);
//        ibRight.setOnClickListener(this);
        meeting_right_tv.setVisibility(View.VISIBLE);
        tvTitle.setText("会议信息");
        initDialog();

        nowMeetingWeek = Calendar.getInstance(Locale.SIMPLIFIED_CHINESE).getTime();
        inquireMeetingListWeekly(nowMeetingWeek);

        lvMeeting.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Bundle bundle = new Bundle();
                Intent intent = new Intent(aty, Act_MeetingDetail.class);
                bundle.putString("meetingId", meetingBeanDates.get(position).getMeetingId());
                bundle.putInt("type", 0);
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });
    }

    private void initDialog() {
        searchDialog = new SearchDialog(this, "请输入会议主题");
        searchDialog.setSearchListener(new SearchDialog.OnSearchListener() {
            @Override
            public void onSearch(String stringSearch) {
                searchTheme = stringSearch;
                inquireMeetingListWeekly(nowMeetingWeek);
            }
        });

        navDialog = new Dialog(this, R.style.dialog4style);
        View dialogView = LayoutInflater.from(this).inflate(R.layout.dialog_meeting_nav, null);
        relevanceT = (TextView) dialogView.findViewById(R.id.tv_relevance);
        View searchItem = dialogView.findViewById(R.id.item_search);
        View aboutItem = dialogView.findViewById(R.id.item_about);
        navDialog.setContentView(dialogView);
        searchItem.setOnClickListener(this);
        aboutItem.setOnClickListener(this);

        navDialog.setCanceledOnTouchOutside(true);
        Window dialogWindow = navDialog.getWindow();
        dialogWindow.setWindowAnimations(R.style.DialogAnimFade);
        WindowManager.LayoutParams params = dialogWindow.getAttributes();
        params.gravity = Gravity.TOP;
        params.width = ScreenUtil.getScreenWidth(this);
        dialogWindow.setAttributes(params);
    }

    @Override
    @OnClick({R.id.back, R.id.iv_preview, R.id.iv_next, R.id.right})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
//            case R.id.ib_right:
//                if(!navDialog.isShowing()) {
//                    if(interrelated) {
//                        relevanceT.setText("全部");
//                    } else {
//                        relevanceT.setText("与我相关");
//                    }
//                    navDialog.show();
//                }
//                break;
            case R.id.item_search:
                navDialog.dismiss();
                if (!searchDialog.isShowing()) {
                    searchDialog.setSearchText(searchTheme);
                    searchDialog.show();
                }
                break;
            case R.id.item_about:
                navDialog.dismiss();
                interrelated = !interrelated;
                inquireMeetingListWeekly(nowMeetingWeek);
                break;
            case R.id.iv_preview:
                previewWeekMeeting();
                break;
            case R.id.iv_next:
                nextWeekMeeting();
                break;
            case R.id.right:
                interrelated = !interrelated;
                if (interrelated) {
                    meeting_right_tv.setText("全部");
                } else {
                    meeting_right_tv.setText("与我相关");
                }
                inquireMeetingListWeekly(nowMeetingWeek);
                break;
        }
    }

    private void inquireMeetingListWeekly(final Date meetingDate) {
        nowMeetingWeek = meetingDate;

        tvWeekTitle.setText(DateUtil.getWeekOfMonth(nowMeetingWeek));
        String weekDateString = String.format("%s-%s",
                DateFormat.format("MM.dd", DateUtil.getFirstDayOfWeek(nowMeetingWeek)),
                DateFormat.format("MM.dd", DateUtil.getLastDayOfWeek(nowMeetingWeek)));
        tvWeekDate.setText(weekDateString);

        Map<String, String> params = new HashMap<>();
        params.put("sessionUuid", PreferenceHelper.readString(aty, AppConfig.USER_INFO, AppConfig.SESSION_ID));
//        params.put("searchDate", DateFormat.format("yyyy-MM-dd", meetingDate).toString());
        params.put("startDate", DateFormat.format("yyyy-MM-dd", DateUtil.getFirstDayOfWeek(nowMeetingWeek)).toString());
        params.put("endDate", DateFormat.format("yyyy-MM-dd", DateUtil.getLastDayOfWeek(nowMeetingWeek)).toString());
        params.put("meetingName", searchTheme);
        params.put("accountFlag", String.valueOf(interrelated));
        presenter.getMeeting(params);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        navDialog.dismiss();
        searchDialog.dismiss();
    }

    private void previewWeekMeeting() {
        Calendar calendar = Calendar.getInstance(Locale.SIMPLIFIED_CHINESE);
        calendar.setTime(nowMeetingWeek);
        calendar.add(Calendar.DATE, -7);
        inquireMeetingListWeekly(calendar.getTime());
    }

    private void nextWeekMeeting() {
        Calendar calendar = Calendar.getInstance(Locale.SIMPLIFIED_CHINESE);
        calendar.setTime(nowMeetingWeek);
        calendar.add(Calendar.DATE, 7);
        inquireMeetingListWeekly(calendar.getTime());
    }


    @Override
    public void showMeetingList(MeetingBean list) {
        if (meetingBeanDates != null) {
            if (meetingBeanDates.size() > 0) {
                meetingBeanDates.clear();
            }
        }
        if (list.getStatus().equals("200")) {
            meetingBeanDates = list.getDatas();
            adapter = new MeetingAdapter(lvMeeting, meetingBeanDates, R.layout.item_meeting);
            lvMeeting.setAdapter(adapter);
        }
    }
}
